/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { exec, spawn } = require('child_process');

console.log('打包');

var cmdPython = 'npm run build';
var publish =
  'docker buildx build --platform linux/amd64 -t ccr.ccs.tencentyun.com/dyh_weapp/rose:latest . --push';

async function script() {
  let result = false;
  try {
    result = await new Promise((resole) => {
      const dockerProcess = spawn(cmdPython, { shell: true });

      dockerProcess.stdout.on('data', (data) => {
        console.log(data.toString()); // 打印子进程的输出
      });

      dockerProcess.stderr.on('data', (data) => {
        console.error(data.toString()); // 打印子进程的错误输出
      });

      dockerProcess.on('close', (code) => {
        resole(true);
      });
    });
  } catch (e) {
    console.log(e);
  }
  try {
    if (!result) {
      console.log('打包失败');
      return;
    }
    console.log('发布');

    await new Promise((resole) => {
      const dockerProcess = spawn(publish, { shell: true });

      dockerProcess.stdout.on('data', (data) => {
        console.log(data.toString()); // 打印子进程的输出
      });

      dockerProcess.stderr.on('data', (data) => {
        console.error(data.toString()); // 打印子进程的错误输出
      });

      dockerProcess.on('close', (code) => {
        resole(true);
      });
    });
  } catch (e) {
    console.log(e);
  }
}
script();
